package com.cc8w.home.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cc8w.entity.UserEntity;
import com.cc8w.home.service.UserService;

@Controller
@RequestMapping("/data")
public class DataController {
	
	@Autowired
	public UserService userService;
	
	@RequestMapping("/getuser")
	public ModelAndView getUserInfo(HttpServletRequest req,ModelAndView mv) {
		//System.out.println(req.getParameter("abc"));
		UserEntity user = userService.queryUserByUserName1("admin");
		System.out.println(user);
		
		mv.setViewName("/data/getuser");
		mv.addObject("user", user);
		return mv;
		
	}
	@RequestMapping("/adduser")
	public String addUser(HttpServletRequest req,UserEntity user){
		System.out.println(user);
		if("".equals(user.getUserName())|| user.getUserName()==null){
			return "/data/adduser";
		}



		//model.addAttribute("id", student.getId());

		return "/data/insertuser";


	}

	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public @ResponseBody String uploadFile(@RequestParam("user") String user,@RequestParam("file") CommonsMultipartFile file) throws IOException{	
		long  startTime=System.currentTimeMillis();//用来检测程序运行时间
		
		System.out.println(user);//表单的文本字段		
		System.out.println(file);//表单提交过来的文件

		System.out.println("fileName："+file.getOriginalFilename());
		
		//1.获得io流
		InputStream is = file.getInputStream();
		//2.创建一个输出流，把获得的io流写到硬盘
		OutputStream os = new FileOutputStream(new File("C:\\1.txt"));
		
		byte[] bs = new byte[1024];
		int len = -1;
		while((len = is.read(bs))!=-1) {
			os.write(bs,0,len);
		}
		
		os.close();
		is.close();

		return null;
	}



	@RequestMapping(value = "/uploadFile1", method = RequestMethod.POST)
	public @ResponseBody String uploadFile1(HttpServletRequest req){

		//System.out.println(file);

		return null;
	}

	
	
	@RequestMapping("/test01")
	public void test01(UserEntity user) {
		//http://localhost:8080/java_security/data/test01?id=1&userName=aaaa&cname=lee&email=569811586@qq.com
		System.out.println(user);

		
		
	}
	
	
	
	

}
